$(document).ready(function(){
	getActivity();
});

function getActivity(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        "scrollX": true,
        ajax:{
            url:'/push-goods/get-push-goods',
            type:'post',
            data:function(d){
            	d.goods_name_cn = $('#goods_name_cn').val();
            	d.reservationtime = $('#reservationtime').val();
            	d.consult = $('#consult').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "update_time", "bSortable": true},
            { "data": "item_no", "bSortable": false },
            { "data": "list_price", "bSortable": false },
            { "data": "item_price", "bSortable": false },
            { "data": "item_desc", "bSortable": false },
            { "data": "itemtype", "bSortable": false },
            { "data": "item_group", "bSortable": false },
            { "data": "item_spec", "bSortable": false },
            { "data": "item_stuff", "bSortable": false },
            { "data": "brand_code", "bSortable": false },
            { "data": "brand_ename", "bSortable": false },
            { "data": "brand_cname", "bSortable": false },
            { "data": "item_sf", "bSortable": false },
            { "data": "minin_qty", "bSortable": false },
            { "data": "color", "bSortable": false },
            { "data": "item_country", "bSortable": false },
            { "data": "item_state", "bSortable": false },
            { "data": "itemname", "bSortable": false },
            { "data": "itemmodel", "bSortable": false },
            { "data": "itemseries", "bSortable": false },
            { "data": "curinccode", "bSortable": false },
            { "data": "preinccode", "bSortable": false },
            { "data": "itememployee", "bSortable": false },
            { "data": "consult", "bSortable": false }
        ],
        columnDefs: [
        	{
                "targets": [23],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return data == '0' ? '<a href="javascript:" onclick="viewInfo('+ full.id +')">查阅</a>' : '<span class="text-success">已查阅</span>';
                }
            },
        ]
    });
}

function table_list_reload(){
	var table = $('#table_list').DataTable();
    table.ajax.reload();
}

$('#reservationtime').daterangepicker({
    language:  'zh-CN',
    timePicker: true,
    timePickerIncrement: 30,
    format: 'YYYY-MM-DD',
    locale: {
        format: 'YYYY-MM-DD',
        daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
        monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
    }
});

$('#reset_button').on('click',function(){
	var str_date = show();
	$('#goods_name_cn').val('');
	$('#reservationtime').val(str_date+' - '+str_date);
	$('#consult').val(0);
	table_list_reload();
});


function show() {
    var date = new Date();
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    return currentdate;
}

function viewInfo(id){
	$.post('/push-goods/get-push-goods-detail',{id:id},success,'json');
    return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200)
        {
        	var res = data.val;
        	var id = $('#push-goods-modal');
        	id.modal('show');
        	id.find('.item_no').text(res.item_no);
        	id.find('.list_price').text(res.list_price);
        	id.find('.item_price').text(res.item_price);
        	id.find('.itemtype').text(res.itemtype);
        	id.find('.item_desc').text(res.item_desc);
        	id.find('.item_group').text(res.item_group);
        	id.find('.item_spec').text(res.item_spec);
        	id.find('.item_stuff').text(res.item_stuff);
        	id.find('.item_u_m').text(res.item_u_m);
        	id.find('.brand_ename').text(res.brand_ename);
        	id.find('.brand_cname').text(res.brand_cname);
        	id.find('.item_sf').text(res.item_sf);
        	id.find('.minin_qty').text(res.minin_qty);
        	id.find('.color').text(res.color);
        	id.find('.item_country').text(res.item_country);
        	id.find('.item_state').text(res.item_state);
        	id.find('.itemname').text(res.itemname);
        	id.find('.itemmodel').text(res.itemmodel);
        	id.find('.itemseries').text(res.itemseries);
        	id.find('.curinccode').text(res.curinccode);
        	id.find('.preinccode').text(res.preinccode);
        	id.find('.itememployee').text(res.itememployee);
        	$('#line_sync_id').val(res.id);
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
}

$('#push-goods-modal').on('click','.ajax_post_tag',function(){
	var line_sync_id = $('#line_sync_id').val();
	$.post('/push-goods/change-push-goods-consult',{line_sync_id:line_sync_id},success,'json');
	function success(data){
		$.jBox.close();
        if(data.code == 200)
        {
        	$('#push-goods-modal').modal('hide');
        	table_list_reload();
        }else{
            $.jBox.error(data.val, '错误');
        }
	}
});
